#include "hospital.h"
#include "ui_hospital.h"


Hospital::Hospital(QWidget *parent, int id) :
    QWidget(parent),
    ui(new Ui::Hospital)
{
    ui->setupUi(this);
    this->id = id;

    this->setWindowFlags(Qt::FramelessWindowHint);      // 隐藏边框
    this->setAttribute(Qt::WA_TranslucentBackground);     // 背景透明


    upStair = false;  // 默认在楼下

    // 提示对话框设置： (默认关闭)
    ui->communicate->hide();
    order = 1;
    action = false;

    // 初始权限
    set_canOpen();

    // 信号与槽：
    connect(ui->tips, SIGNAL(clicked()), this, SLOT(communicate_act()));

    // 开启数据库
    dbase = new DbOperator();
    dbase->OpenDb();

    trainerMsg = dbase->searchTrainerMsg(id);
    init_Pokemon_Msg();

}

Hospital::~Hospital()
{
    delete ui;
    if(dbase != 0) {
        delete dbase;
        dbase = 0;
    }
}

void Hospital::closeEvent(QCloseEvent *) {
    emit ExitHospital();  // 关闭时发射信号
}

void Hospital::mousePressEvent(QMouseEvent *)
{
    if (action) {
        switch (order) {
        case 1:
            ui->text_area->setText(content[1]);
            order++;
            break;
        case 2:
            ui->text_area->setText(content[3]);
            order++;
            break;
        default:
            order = 1;
            ui->Title->setText("");
            ui->text_area->setText("");
            ui->console->setText("");
            ui->communicate->hide();
            action = false;    // 结束
            set_canOpen();
            break;
        }
    }
}

void Hospital::init_Pokemon_Msg()
{
    // 清空原本中的数据
    QVector<pokemon> nulltemp;
    qSwap(nulltemp, pokemonMsg);
    // 加载宝可梦数据
    QStringList pokemon_idno_list = trainerMsg.idno.split(".");   // 拥有的pokemon编号
    pokemon temp;
    // 读取trainer中的idno，在pokemon表中查询放入向量中
    for(int i = 0; i < pokemon_idno_list.length(); i++) {
        QString ido = pokemon_idno_list.at(i);
        if (ido != QString("0")) {
            int idno = ido.toInt();
            temp = dbase->searchPokemonMsg(idno);
            pokemonMsg.push_back(temp);
            qDebug() << "insert pokemsg success";
        }
        else {
            dbase->setNullPokemon(temp);
            pokemonMsg.push_back(temp);
            qDebug() << "insert pokemsg success null";
        }
    }
}


void Hospital::on_upstair_clicked()
{
    if(canOpen) {
        // 使用这个函数时原来的样式表都会被清理，为重新设置
        ui->bgimg->setStyleSheet("border-image: url(:/bgi/images/bgd/hospital_up.png);");  // 设置楼上图片
        ui->cure->hide();
        ui->tips->hide();
        upStair = true;
        set_canOpen();
        return;
    }
    if(upStair) {
        ui->bgimg->setStyleSheet("border-image: url(:/bgi/images/bgd/hospital.png);");  // 设置楼上图片
        ui->cure->show();
        ui->tips->show();
        upStair = false;
        set_canOpen();
    }

}

void Hospital::communicate_act()
{
    if(canOpen) {
        ui->communicate->show();
        ui->Title->setText(content[0]);    // 标题
        ui->console->setText(content[2]);    // 右下角提示
        action = true;      // 开启鼠标检测
        set_canOpen();
    }
}

void Hospital::set_canOpen()
{
    canOpen = !(action || upStair);
}

void Hospital::on_cure_clicked()
{
    int ret = QMessageBox::question(this, tr("确认"),
                                    tr("确认治疗所有宠物吗？"),
                                    QMessageBox::Yes,
                                    QMessageBox::No);
    if(ret == QMessageBox::Yes) {
        pokemon temp;
        for (int i = 0; i < 3; i++) {      // 遍历所有宠物对象，更新数据库中数据
            temp = pokemonMsg.at(i);
            if(temp.id != 0){
                dbase->cureMaxHP(temp.idno);
            }
        }
        QMessageBox::information(this, tr("成功"),
                                 tr("所有宠物已治疗"),
                                 QMessageBox::Ok);
    }
}

void Hospital::on_closebutton_clicked()
{
    this->close();
    delete dbase;
    dbase = 0;
}

void Hospital::on_pokemon_clicked()
{
    pokemonSurface = new Widget(this, id);
    pokemonSurface->move(270, 150);
    pokemonSurface->show();
    pokemonSurface->on_pet_1_clicked();
}














